<%@ page language="java"
	import="java.util.*,edu.cnu.tpts.bean.*,edu.cnu.tpts.dao.*,edu.cnu.tpts.dao.impl.*"
	pageEncoding="UTF-8"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'doregister_tea.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<body>
		<%
			request.setCharacterEncoding("utf-8");
			int id=0;
			try{
			id = Integer.parseInt(request.getParameter("id"));
			}catch(Exception e){
			request.setAttribute("iderror","必须输入数字格式");
			}
			String password = request.getParameter("password");
			String repassword = request.getParameter("repassword");
			String loginname = request.getParameter("loginname");
			String realname = request.getParameter("realname");
			String sex = request.getParameter("sex");
			String tea_department = request.getParameter("tea_department");
		
			
			TeacherDao teacherDao =new TeacherDaoImpl();
			Teacher teacher1=teacherDao.findById(id);
			Teacher teacher2=teacherDao.findByName(loginname,false);
			if(teacher1!=null){
				request.setAttribute("iderror","该学号已经被使用");
				request.getRequestDispatcher("/teacher/register_tea.jsp").forward(request,response);
			}else if(teacher2!=null){
				request.setAttribute("namesame","该登录名已经被使用");
				request.getRequestDispatcher("/teacher/register_tea.jsp").forward(request,response);
			}else if (!password.equals(repassword)) {
				request.setAttribute("pwdsame", "密码和确认密码不一致");
				request.getRequestDispatcher("/teacher/register_tea.jsp")
						.forward(request, response);
			} else {
				Teacher teacher = new Teacher();
				teacher.setId(id);
				teacher.setPassword(password);
				teacher.setLoginName(loginname);
				teacher.setRealName(realname);
				teacher.setSex(sex);
				teacher.setTeaDepartment(tea_department);
				teacherDao.addTeacher(teacher);
				String type=(String)session.getAttribute("type");	
				if(type==null||type.equals("teacher")||type.equals("")){//如果是教师注册成功，将直接进入登陆成功状态，需要将其设为当前用户					
					session.setAttribute("type","teacher");
					session.setAttribute("user",teacher);
					request.getRequestDispatcher("/teacher/index.jsp").forward(request,response);
					
					}
				else //管理员添加教师信息
					request.getRequestDispatcher("/admin/admin_tea.jsp").forward(request,response);
			}
		%>

	</body>
</html>
